package stack

import (
	"container/list"
)

// https://leetcode.cn/problems/valid-parentheses/ leetcode-20
func isValid(s string) bool {
	stack := list.New()
	check := func(expect int32, e *list.Element) bool {
		if e == nil {
			return true
		}
		return e.Value.(int32) != expect
	}
	for _, v := range s {
		switch v {
		case '(', '{', '[':
			stack.PushFront(int32(v))
		case ')':
			val := stack.Front()
			if check('(', val) {
				return false
			}
			stack.Remove(val)
		case '}':
			val := stack.Front()
			if check('{', val) {
				return false
			}
			stack.Remove(val)
		case ']':
			val := stack.Front()
			if check('[', val) {
				return false
			}
			stack.Remove(val)
		}
	}
	return stack.Len() == 0
}
